package com.hainatong.shiphelper.module.warehouse.viewmodel;

import com.hainatong.shiphelper.api.ApiResult;
import com.hainatong.shiphelper.api.ApiService;
import com.hainatong.shiphelper.api.VMObserver;
import com.hainatong.shiphelper.module.warehouse.entity.ChuKuRaramBean;
import com.hainatong.mvvmbase.base.BaseViewModel;
import com.hainatong.mvvmbase.base.LiveResult;
import com.hainatong.mvvmbase.http.RxSchedulersUtils;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import io.reactivex.disposables.Disposable;

public class LYRecordDetailViewModel extends BaseViewModel
{

    public LYRecordDetailViewModel(@NonNull Application application)
    {
        super(application);
    }

    private MutableLiveData<LiveResult<Object>> chukuResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<Object>> getChukuResult()
    {
        return chukuResult;
    }

    /**
     *  出库 任务
     * @param chukuGoods
     */
    public void chukuTask(ChuKuRaramBean[] chukuGoods )
    {
        ApiService.Builder.getChanBoServer()
                .chuKuTask( chukuGoods )
                .compose( RxSchedulersUtils.Obs_io_main() )
                .subscribe(new VMObserver<ApiResult<Object>>()
                {
                    @Override
                    public void onStart(Disposable d)
                    {
                        addDisposable( d );
                    }

                    @Override
                    public void onError(int code, String errorMsg)
                    {
                        getChukuResult().postValue( new LiveResult<>( false ,errorMsg ) );
                    }

                    @Override
                    public void onSuccess(ApiResult<Object> result)
                    {
                        if( result.isOk() )
                        {
                            getChukuResult().postValue( new LiveResult<>( true , result.getData() ) );
                        }
                        else
                        {
                            getChukuResult().postValue( new LiveResult<>( false , result.getMessage() ) );
                        }
                    }
                });
    }



}
